/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.magnifier;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQMagnifierUtils
{

	/**
	 * Create a JQMagnifierBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQMagnifierBuilder
	 */
	public static JQMagnifierBuilder create(String id, String key)
	{
		return new JQMagnifierBuilder(new JQMagnifier(id, (key == null ? JQMagnifierPlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQMagnifierBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQMagnifierBuilder
	 */
	public static JQMagnifierBuilder create(String id)
	{
		return new JQMagnifierBuilder(new JQMagnifier(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQMagnifierBuilder
	{
		/** JQuery component */
		private JQMagnifier component;
		/** JQuery default options */
		private JQMagnifierOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQMagnifierBuilder(JQMagnifier component)
		{
			this.component = component;
			this.options = new JQMagnifierOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the axis option value
		 * 
		 * @param axis
		 *            magnifier axis
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder axis(String axis)
		{
			options.setAxis(axis);
			return this;
		}

		/**
		 * Sets the items option value
		 * 
		 * @param items
		 *            magnifier items
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder items(String items)
		{
			options.setItems(items);
			return this;
		}

		/**
		 * Sets the baseline option value
		 * 
		 * @param baseline
		 *            magnifier baseline
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder baseline(int baseline)
		{
			options.setBaseline(baseline);
			return this;
		}

		/**
		 * Sets the distance option value
		 * 
		 * @param distance
		 *            magnifier distance
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder distance(int distance)
		{
			options.setDistance(distance);
			return this;
		}

		/**
		 * Sets the magnification option value
		 * 
		 * @param magnification
		 *            magnifier magnification
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder magnification(float magnification)
		{
			options.setMagnification(magnification);
			return this;
		}

		/**
		 * Sets the opacityMin option value
		 * 
		 * @param opacityMin
		 *            magnifier min opacity
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder opacityMin(int opacityMin)
		{
			options.setOpacityMin(opacityMin);
			return this;
		}

		/**
		 * Sets the opacityMax option value
		 * 
		 * @param opacityMax
		 *            magnifier max opacity
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder opacityMax(int opacityMax)
		{
			options.setOpacityMax(opacityMax);
			return this;
		}

		/**
		 * Sets the zIndex option value
		 * 
		 * @param index
		 *            magnifier zIndex
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder zIndex(int index)
		{
			options.setZIndex(index);
			return this;
		}

		/**
		 * Sets the verticalLine option value
		 * 
		 * @param verticalLine
		 *            magnifier verticalLine
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder verticalLine(float verticalLine)
		{
			options.setVerticalLine(verticalLine);
			return this;
		}

		/**
		 * Sets the overlap option value
		 * 
		 * @param overlap
		 *            magnifier overlap
		 * @return JQMagnifierBuilder
		 */
		public JQMagnifierBuilder overlap(boolean overlap)
		{
			options.setOverlap(overlap);
			return this;
		}

		/**
		 * @return JQMagnifier
		 */
		public JQMagnifier build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}